<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货单号" prop="returnNo">
        <el-input
          v-model="queryParams.returnNo"
          placeholder="请输入退货单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报id" prop="reportId">
        <el-input
          v-model="queryParams.reportId"
          placeholder="请输入实销上报id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商id" prop="dealerId">
        <el-input
          v-model="queryParams.dealerId"
          placeholder="请输入经销商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商名" prop="dealerName">
        <el-input
          v-model="queryParams.dealerName"
          placeholder="请输入经销商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货状态" prop="applyStatus">
        <el-select v-model="queryParams.applyStatus" placeholder="请选择退货状态" clearable>
          <el-option
            v-for="dict in dict.type.whole_return_apply_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三包质量" prop="isQualityProblem">
        <el-select v-model="queryParams.isQualityProblem" placeholder="请选择是否符合三包质量" clearable>
          <el-option
            v-for="dict in dict.type.whole_return_quality_problem"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库日期" prop="storageDate">
        <el-date-picker clearable
          v-model="queryParams.storageDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退车仓库" prop="returnWarehouse">
        <el-select v-model="queryParams.returnWarehouse" placeholder="请选择退车入库仓库" clearable>
          <el-option
            v-for="dict in dict.type.warehouse_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退款账户" prop="returnAccount">
        <el-input
          v-model="queryParams.returnAccount"
          placeholder="请输入退款账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="returnAmount">
        <el-input
          v-model="queryParams.returnAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退车用户" prop="returnName">
        <el-input
          v-model="queryParams.returnName"
          placeholder="请输入退车用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退车时间" prop="returnDate">
        <el-date-picker clearable
          v-model="queryParams.returnDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择退车时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请日期" prop="applyDate">
        <el-date-picker clearable
          v-model="queryParams.applyDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请人名" prop="applyPersonName">
        <el-input
          v-model="queryParams.applyPersonName"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人名" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditorTime">
        <el-date-picker clearable
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['whole:return:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['whole:return:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['whole:return:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['whole:return:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="returnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="false"/>
      <el-table-column label="退货单id" align="center" prop="id" />
      <el-table-column label="退货单号" align="center" prop="returnNo" />
      <el-table-column label="上报id" align="center" prop="reportId" />
      <el-table-column label="经销商id" align="center" prop="dealerId" />
      <el-table-column label="经销商名称" align="center" prop="dealerName" />
      <el-table-column label="退货状态" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_return_apply_status" :value="scope.row.applyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否符合三包质量" align="center" prop="isQualityProblem">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_return_quality_problem" :value="scope.row.isQualityProblem"/>
        </template>
      </el-table-column>
      <el-table-column label="入库日期" align="center" prop="storageDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.storageDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退车仓库" align="center" prop="returnWarehouse">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warehouse_list" :value="scope.row.returnWarehouse"/>
        </template>
      </el-table-column>
      <el-table-column label="退款账户" align="center" prop="returnAccount" />
      <el-table-column label="退款金额" align="center" prop="returnAmount" />
      <el-table-column label="退车用户" align="center" prop="returnName" />
      <el-table-column label="退车时间" align="center" prop="returnDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退车说明" align="center" prop="returnDesc" />
      <el-table-column label="申请日期" align="center" prop="applyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人姓名" align="center" prop="applyPersonName" />
      <el-table-column label="审核人名称" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="auditorDesc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-show="(scope.row.applyStatus == 0) || (scope.row.applyStatus == 1) || (scope.row.applyStatus == 2)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['whole:return:edit']"
          >审核</el-button>
          <el-button
            v-show="scope.row.applyStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['whole:return:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退车管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退货单号" prop="returnNo">
          <el-input v-model="form.returnNo" placeholder="请输入退货单号(若未填写，后台自动生成)" :disabled="title!='添加退车管理'"/>
        </el-form-item>
<!--        <el-form-item label="上报id" prop="reportId">-->
<!--          <el-input v-model="form.reportId" placeholder="请输入实销上报id" />-->
<!--        </el-form-item>-->
        <el-form-item label="退货状态" prop="applyStatus">
          <el-select v-model="form.applyStatus" placeholder="请选择退货状态" >
            <el-option
              v-for="dict in dict.type.whole_return_apply_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否符合三包质量" prop="isQualityProblem">
          <el-select v-model="form.isQualityProblem" placeholder="请选择是否符合三包质量" :disabled="title!='添加退车管理'">
            <el-option
              v-for="dict in dict.type.whole_return_quality_problem"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库日期" prop="storageDate">
          <el-date-picker clearable
            v-model="form.storageDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入库日期"
            :disabled="form.applyStatus!=2"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="退车仓库" prop="returnWarehouse">
          <el-select v-model="form.returnWarehouse" placeholder="请选择退车入库仓库" :disabled="form.applyStatus!=2">
            <el-option
              v-for="dict in dict.type.warehouse_list"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款账户" prop="returnAccount">
          <el-input v-model="form.returnAccount" placeholder="请输入退款账户" :disabled="title!='添加退车管理'" />
        </el-form-item>
        <el-form-item label="退款金额" prop="returnAmount">
          <el-input v-model="form.returnAmount" placeholder="请输入退款金额" :disabled="title!='添加退车管理'" />
        </el-form-item>
        <el-form-item label="退车用户" prop="returnName">
          <el-input v-model="form.returnName" placeholder="请输入退车用户姓名" :disabled="title!='添加退车管理'"/>
        </el-form-item>
        <el-form-item label="退车时间" prop="returnDate" >
          <el-date-picker clearable
            v-model="form.returnDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退车时间"
            :disabled="title!='添加退车管理'"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="退车说明" prop="returnDesc">
          <el-input v-model="form.returnDesc" type="textarea" placeholder="请输入内容" :disabled="title!='添加退车管理'"/>
        </el-form-item>
        <el-form-item label="审批意见" prop="auditorDesc">
          <el-input v-model="form.auditorDesc" type="textarea" placeholder="请输入内容" :disabled="title=='添加退车管理'"/>
        </el-form-item>
        <el-divider content-position="center">已实销上报的单</el-divider>
        <!-- 查询已实销上报单 -->
        <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange01">
          <el-table-column v-if="title=='添加退车管理'" type="selection" width="55" align="center" />
          <el-table-column label="上报id" align="center" prop="id" />
          <el-table-column label="车辆vin" align="center" prop="vehicleVin" />
          <el-table-column label="零售单号" align="center" prop="retailOrderNo" />
          <el-table-column label="经销商名称" align="center" prop="dealerName" />
          <el-table-column label="客户类型" align="center" prop="customerType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.whole_sale_customer_type" :value="scope.row.customerType"/>
            </template>
          </el-table-column>
          <el-table-column label="PDI 日期" align="center" prop="pdiDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.pdiDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="销售日期" align="center" prop="saleDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="实销上报日期" align="center" prop="reportDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="上报人姓名" align="center" prop="reportPersonName" />
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReturn, getReturn, delReturn, addReturn, updateReturn } from "@/api/whole/return";
import {getReport, listReport} from "@/api/whole/reportQuery";

export default {
  name: "Return",
  dicts: ['whole_return_quality_problem', 'warehouse_list', 'whole_return_apply_status'],
  data() {
    return {
      //实销上报回显id
      reportIdTemp: null,
      // 实销上报表格数据
      reportList: [],
      //实销上报的一条数据的id
      reportIds:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退车管理表格数据
      returnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnNo: null,
        reportId: null,
        dealerId: null,
        dealerName: null,
        applyStatus: null,
        isQualityProblem: null,
        storageDate: null,
        returnWarehouse: null,
        returnAccount: null,
        returnAmount: null,
        returnName: null,
        returnDate: null,
        returnDesc: null,
        applyDate: null,
        applyPersonName: null,
        auditorName: null,
        auditorTime: null,
        auditorDesc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // returnNo: [
        //   { required: true, message: "退货单号不能为空", trigger: "blur" }
        // ],
        applyStatus: [
          { required: true, message: "退货状态不能为空", trigger: "blur" }
        ],
        isQualityProblem: [
          { required: true, message: "是否符合三包质量不能为空", trigger: "blur" }
        ],
        returnAccount: [
          { required: true, message: "退款账户不能为空", trigger: "blur" }
        ],
        returnAmount: [
          { required: true, message: "退款金额不能为空", trigger: "blur" }
        ],
        returnName: [
          { required: true, message: "退车用户不能为空", trigger: "blur" }
        ],
        returnDate: [
          { required: true, message: "退车时间不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退车管理列表 */
    getList() {
      this.loading = true;
      listReturn(this.queryParams).then(response => {
        this.returnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams= {
        pageNum: 1,
          pageSize: 10,
          returnNo: null,
          reportId: null,
          dealerId: null,
          dealerName: null,
          applyStatus: null,
          isQualityProblem: null,
          storageDate: null,
          returnWarehouse: null,
          returnAccount: null,
          returnAmount: null,
          returnName: null,
          returnDate: null,
          returnDesc: null,
          applyDate: null,
          applyPersonName: null,
          auditorName: null,
          auditorTime: null,
          auditorDesc: null,
      },

      this.form = {
        id: null,
        returnNo: null,
        reportId: null,
        dealerId: null,
        dealerName: null,
        applyStatus: null,
        isQualityProblem: null,
        storageDate: null,
        returnWarehouse: null,
        returnAccount: null,
        returnAmount: null,
        returnName: null,
        returnDate: null,
        returnDesc: null,
        applyDate: null,
        applyPersonId: null,
        applyPersonName: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
        auditorDesc: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      reportIdTemp: null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 实销上报单多选框选中数据
    handleSelectionChange01(selection) {
      this.reportIds = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退车管理";
      // 查询车辆库存表
      this.getReportList();
    },
    getReportList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReturn(id).then(response => {
        this.form = response.data;
        this.reportIdTemp = response.data.reportId;
        this.open = true;
        this.title = "修改退车管理";
        // 查询车辆库存表
        this.getReportListUpdate(this.reportIdTemp);
      });
    },
    getReportListUpdate(reportId) {
      this.loading = true;
      this.queryParams.id = reportId;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReturn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //限制只能选一个
            if(this.reportIds.length != 1){
              this.$message.error('单次必须且只能选择一台车辆退车！');
              return;
            }
            // console.log(this.reportIds)
            this.form.reportId = this.reportIds[0];
            addReturn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.applyStatus!=0){
        this.$message.error("只能删除未提交的退车单！");
        return;
      }
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除退车管理编号为"' + ids + '"的数据项？').then(function() {
        return delReturn(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('whole/return/export', {
        ...this.queryParams
      }, `return_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
